package com.tao.controller;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.tao.factorys.SubmittableFactory;
import com.tao.pojo.Submittable;
import com.tao.service.SubmittableService;
import com.tao.service.impl.Mq.MiaoshaSendService;
import com.tao.service.impl.Mq.SubmitSendMqService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: alubus
 * @Date: 2021/11/12 17:22
 * @Package: com.tao.controller
 */
@RestController()
@RequestMapping("/submitTable")
public class SubmitTableController {
    @Resource
    SubmittableService submittableService;

    //  /Submit/insertOne
    @Resource
    SubmittableFactory submittableFactory;

    @Resource
    SubmitSendMqService submitSendMqService;

    @RequestMapping("/insertOne")
    public String InsertOne(){
        Submittable submittable = submittableFactory.getSubmittable();
        submittableService.save(submittable);
        return "success";
    }

    /**
     *
     * @return 全部的秒杀提交表
     */
    @RequestMapping("/selectAll")
    public List<Submittable> selectAll(){
        return submittableService.list();
    }

    @RequestMapping("/select/{id}")
    public Submittable selectOne(@PathVariable("id") Long id){
        return submittableService.getById(id);
    }

    @RequestMapping("/delete/{id}")
    public boolean deleteById(@PathVariable("id") Long id){
        return submittableService.removeById(id);
    }



    /*
    * 订单 死信队列的
    * */

    @RequestMapping("/addSubmitTable/{aid}/{qid}")
    public String sendSubmitID(@PathVariable("aid") Long aid,@PathVariable("qid")  Long qid){
        Submittable submittable = submittableFactory.getSubmittable(qid, aid);
        submittableService.sendSubmitTableToMq(submittable);
        return "sendSuccess";
    }

    @RequestMapping("/dealSubmitTable/{id}")
    public String dealSubmitTable(@PathVariable  Long id){
        boolean b = submittableService.receiveMsg(id);

        if (b){
            return "sendSuccess";

        }
        return "已过期 提交失败";
    }

    @RequestMapping("/selectByAid/{aid}")
    public JSONObject selectByAid(@PathVariable  Long aid){
        JSONObject oj = new JSONObject();
        List<Submittable> submittables = submittableService.selectByAid(aid);
        oj.put("sta",submittables);
        return oj;
    }



    @RequestMapping("/selectByQid/{qid}")
    public JSONObject selectByQid(@PathVariable  Long qid){
        JSONObject oj = new JSONObject();
        List<Submittable> submittables = submittableService.selectByQid(qid);
        oj.put("stq",submittables);
        return oj;
    }

}
